package cn.javabb.core.service.sys;

import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseService;
import cn.javabb.core.entity.sys.LinkDO;
import cn.javabb.core.mapper.sys.LinkMapper;
import cn.javabb.core.model.StatusEnum;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class LinkService extends BaseService<LinkDO> {
    @Autowired
    private LinkMapper linkMapper;

    public Page queryPageList(int page, int size, LinkDO linkDO){
        Example ex = new Example(LinkDO.class);
        if(!StringUtils.isEmpty(linkDO.getName())){
            ex.createCriteria().andLike("name","%"+linkDO.getName()+"%");
        }
        ex.orderBy("orderNo").asc().orderBy("createTime").desc();
        Page p = this.queryPageByExample(page, size, ex);
        return p;
    }

    public List<LinkDO> list(){
        Example ex = new Example(LinkDO.class);
        ex.createCriteria().andEqualTo("state", StatusEnum.NORMAL.getCode());
        ex.orderBy("createTime").asc().orderBy("orderNo").desc();
        List<LinkDO> list = this.queryListByExample(ex);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list;

    }
    public boolean updateState(String ids,Integer state){
        List<String> idList = ListUtils.stringToListString(ids);
        Example ex = new Example(LinkDO.class);
        ex.createCriteria().andIn("id", idList);
        LinkDO link = new LinkDO();
        link.setState(state);
        return this.linkMapper.updateByExampleSelective(link, ex)>0?true:false;
    }
}
